package com.example.demo1.fragment

import android.content.Context
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.example.demo1.R
import com.example.demo1.databinding.FragmentBaseBinding
import com.example.demo1.reposity.Content
import com.example.utils.loadImage
import kotlinx.coroutines.launch
import java.lang.Exception

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

class BaseFragment : Fragment() {

    private var param1: String? = null
    private var content: Content? = null
    private lateinit var binding: FragmentBaseBinding
    private var currentMediaPosition:Int=0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        arguments?.let {
//            param1 = it.getString(ARG_PARAM1)
//        }

    }
    val mediaPlayer = MediaPlayer()
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        binding.fragmentTextView.text=param1

        Log.d("lifecycleTest", "onViewCreated")
        binding.stopStartButon.setOnClickListener(){
            if (mediaPlayer.isPlaying){
                mediaPlayer.pause()
                currentMediaPosition=mediaPlayer.currentPosition
                binding.stopStartButon.setImageResource(R.drawable.stop_img)
            }
            else {
//                mediaPlayer.seekTo(currentMediaPosition)
                binding.stopStartButon.setImageResource(R.drawable.resum_img)
                mediaPlayer.start()

            }
        }
        binding.againImg.setOnClickListener(){
            binding.stopStartButon.setImageResource(R.drawable.resum_img)
            mediaPlayer.stop()
            startMedia()
        }

    }

   fun startMedia(){
       binding.stopStartButon.setImageResource(R.drawable.resum_img)
       mediaPlayer?.reset()
       val context = requireContext()
       val resId = content!!.audioUrl// 替换为你的 MP3 文件资源 ID
       mediaPlayer?.setDataSource(context, android.net.Uri.parse(resId))
       mediaPlayer?.prepare()
       mediaPlayer?.start()
   }

    override fun onResume() {
        super.onResume()
        Log.d("lifecycleTest", "onResume")
        startMedia()
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d("lifecycleTest", "onAttach")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d("lifecycleTest", "onDestroyView")
        mediaPlayer.release()
    }



    override fun onPause() {
        super.onPause()
        Log.d("lifecycleTest", "onPause")
        mediaPlayer.stop()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("lifecycleTest", "onCreateView")
        binding = FragmentBaseBinding.inflate(layoutInflater)
        lifecycleScope.launch {
            try {
                loadImage(requireContext(),binding.articleImgg, content!!.imgUrl!!.replace("/jpg","").replace("http","https"))
                Log.d("asdnfklafdoqoweigng", content!!.imgUrl!!.replace("/jpg",""))
            }
            catch (e:Exception){
                Log.d("asdnfklafdoqoweigng", "onViewCreated: "+e)
            }
            binding.articleText.setText(content!!.sentence)
        }
        return binding.root
    }

    companion object {

        @JvmStatic
        fun newInstance(list: Content) =
            BaseFragment().apply {
                arguments = Bundle().apply {
                    content=list

                }
            }
    }
}